package com.clickpaas.materialsprocurement.dao.Impl;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.codegen.SrmselectApi;
import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.*;
import com.clickpaas.materialsprocurement.bean.MateralsDTOBean;
import com.clickpaas.materialsprocurement.constant.ConstantFile;
import com.clickpaas.materialsprocurement.dao.SrmSelectDao;
import com.clickpaas.materialsprocurement.entity.SrmSelectEntity;
import com.clickpaas.materialsprocurement.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Repository
public class SrmSelectDaoImpl implements SrmSelectDao {
    @Resource
    private MateralsDTOBean materalsDtoBean;
    @Resource
    private SrmselectApi srmselectApi;
    /**
     * 根据条件查询询价单对象数据
     *
     * @param srmSelectEntity
     */
    @Override
    public List<SrmselectDTOResponse> getSrmSelectByCondtion(SrmSelectEntity srmSelectEntity) {
        SrmselectDTO srmselectDTO = materalsDtoBean.srmselectDTO();
        try {
            SpringUtil.copyPropertiesIgnoreNull(srmSelectEntity,srmselectDTO);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",srmSelectEntity);
        }
        return querySrmSelect(srmselectDTO);
    }

    /**
     * 甄选对象数据入库
     *
     * @param srmSelectEntity
     */
    @Override
    public boolean insertSelectionInfo(SrmSelectEntity srmSelectEntity) {
        SrmselectDTOUpdate srmselectDTOUpdate = materalsDtoBean.srmselectDTOUpdate();
        try {
            SpringUtil.copyPropertiesIgnoreNull(srmSelectEntity,srmselectDTOUpdate);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",srmSelectEntity);
        }
        SaveOrUpdatesrmselectDTO saveOrUpdatesrmselectDTO = new SaveOrUpdatesrmselectDTO();
        saveOrUpdatesrmselectDTO.setQuery(materalsDtoBean.srmselectDTOQuery());
        saveOrUpdatesrmselectDTO.setUpdate(srmselectDTOUpdate);
        return insertData(saveOrUpdatesrmselectDTO);
    }

    private Boolean insertData(SaveOrUpdatesrmselectDTO saveOrUpdatesrmselectDTO){
        try {
            SrmselectSaveOrUpdateDataResponseObject dataResponseString = srmselectApi.saveOrUpdatesrmSelectUsingPOST(saveOrUpdatesrmselectDTO);
            if (null != dataResponseString
                    && ConstantFile._MESSAGE.equals(dataResponseString.getMessage())
                    && null != dataResponseString.getData())
                return true;
        }catch (Exception e){
            log.info("采购订单入库失败：【{}】",e);
        }
        return false;
    }

    private List<SrmselectDTOResponse> querySrmSelect(SrmselectDTO srmselectDTO){
        DataResponseListsrmselectDTO dataResponseListsrmselectDTO = srmselectApi.findsrmSelectUsingPOST(srmselectDTO);
        if(null == dataResponseListsrmselectDTO
                || null == dataResponseListsrmselectDTO.getData()
                || dataResponseListsrmselectDTO.getData().size()<=0
                || !"SUCCESS".equals(dataResponseListsrmselectDTO.getMessage())){
            log.info("当前查询的数据：【{}】",dataResponseListsrmselectDTO);
            return null;
        }
        return dataResponseListsrmselectDTO.getData();
    }
}
